package com.shkco.service.impl;

import com.shkco.domain.Product;
import com.shkco.domain.Store;
import com.shkco.dto.ProductDto;
import com.shkco.feign.StoreService;
import com.shkco.mapper.ProductMapper;
import com.shkco.service.IProductService;
import com.shkco.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private StoreService storeService;

    @Override
    public void add(ProductDto productDto) {
        // 将前端获取到的数据设置到product对象中再保存到数据库
        Product product = new Product();
        product.setName(productDto.getName());
        product.setPrice(productDto.getPrice());
        productMapper.add(product);
        // 将ProductDto中的库存数据保存到库存表中
        Store store = new Store();
        store.setProductId(product.getId());
        store.setNumber(productDto.getNumber());
        AjaxResult add = storeService.add(store);
        if (!add.isSuccess()){
            throw new RuntimeException(add.getMsg());
        }
    }
}
